package com.yeswater.rabbitmq;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.Arrays;
import java.util.concurrent.TimeoutException;

/**
 * 消费者 : 消费者
 * Author : yeswater
 */

public class Consumer {
    public static final String QUEUE_NAME = "mq01";

    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("110.42.184.233");
        connectionFactory.setUsername("admin");
        connectionFactory.setPassword("123");
        // 创建连接
        Connection connection = connectionFactory.newConnection();
        // 这里省略交换机 直接获取信道
        Channel channel = connection.createChannel();

        DeliverCallback deliverCallback = (consumer, message) -> {
            System.out.println(new String(message.getBody()));
        };

        CancelCallback cancelCallback = (consumerTag) -> {
            System.out.println("消息消费被中断");
        };

        /**
         * 1.消费的队列
         * 2.消费成功之后是否要自动应答
         * 3.消费者未成功消费的回调
         * 4.消费者取消消费的回调
         */
        String s = channel.basicConsume(QUEUE_NAME, true, deliverCallback, cancelCallback);
        System.out.println("bc : "+s);
    }
}
